package net.m2technologies.open_arm.transaction;

import net.m2technologies.open_arm.tranreport.ArmApplicationRemoteImpl;
import org.opengroup.arm40.tranreport.ArmApplicationRemote;
import org.opengroup.arm40.tranreport.ArmSystemAddress;
import org.opengroup.arm40.transaction.*;

/**
 * Copyright 2005 Mark Masterson<br> <br> Licensed under the Apache License, Version 2.0 (the "License");<br> you may
 * not use this file except in compliance with the License.<br> You may obtain a copy of the License at<br> <br>
 * http://www.apache.org/licenses/LICENSE-2.0<br> <br> Unless required by applicable law or agreed to in writing,
 * software<br> distributed under the License is distributed on an "AS IS" BASIS,<br> WITHOUT WARRANTIES OR CONDITIONS
 * OF ANY KIND, either express or implied.<br> See the License for the specific language governing permissions and<br>
 * limitations under the License.<br>
 * <p/>
 * <p>Description: </p>
 *
 * @author Mark Masterson
 * @version 0.010
 */
public class ArmApplicationDefinitionImpl extends UniqueObjectImpl implements ArmApplicationDefinition {

    private final ArmIdentityProperties identityProperties;
    private final String applicationName;

    public ArmApplicationDefinitionImpl(final ArmID id,
                                        final ArmIdentityProperties identityProperties,
                                        final String applicationName,
                                        final ArmTransactionFactory creatingFactory) {
        super(id, creatingFactory);
        this.identityProperties = identityProperties;
        this.applicationName = applicationName;
    }

    public void destroy() {
        //noop?
    }

    public ArmIdentityProperties getIdentityProperties() {
        return this.identityProperties;
    }

    public String getName() {
        return this.applicationName;
    }

    public ArmTransactionDefinition getTransactionDefinition(final String uriValue,
                                                             final ArmTransactionFactory creatingFactory) {
        final ArmTransactionDefinition result;
        result = new ArmTransactionDefinitionImpl(creatingFactory.newArmID(null), this, uriValue, creatingFactory);
        return result;
    }

    public ArmApplication getApplication(final String[] contextValues,
                                         final String group,
                                         final String instance,
                                         final ArmTransactionFactory creatingFactory) {
        final ArmApplication result;
        result = new ArmApplicationImpl(this, contextValues, group, instance, creatingFactory);
        return result;
    }

    public ArmApplicationRemote getRemoteApplication(final String[] contextValues,
                                                     final String group,
                                                     final String instance,
                                                     final ArmTransactionFactory creatingFactory,
                                                     final ArmSystemAddress systemAddress) {
        final ArmApplicationRemote result;
        result = new ArmApplicationRemoteImpl(this, contextValues, group, instance, creatingFactory, systemAddress);
        return result;
    }

    public boolean equals(final Object o) {
        if (this == o) {
            return true;
        }
        if (!(o instanceof ArmApplicationDefinitionImpl)) {
            return false;
        }
        if (!super.equals(o)) return false;

        final ArmApplicationDefinitionImpl armApplicationDefinition = (ArmApplicationDefinitionImpl) o;

        if (null != applicationName
            ? !applicationName.equals(armApplicationDefinition.applicationName)
            : null != armApplicationDefinition.applicationName)
            return false;
        if (null != identityProperties
            ? !identityProperties.equals(armApplicationDefinition.identityProperties)
            : null != armApplicationDefinition.identityProperties)
            return false;

        return true;
    }

    public int hashCode() {
        int result = super.hashCode();
        result = HASH_CODE_MAGIC_NUMBER * result + (null != identityProperties ? identityProperties.hashCode() : 0);
        result = HASH_CODE_MAGIC_NUMBER * result + (null != applicationName ? applicationName.hashCode() : 0);
        return result;
    }

}
